package cn.wolfcode.service.impl;

import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);

    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);

    }


    public PageResult<Permission> query(QueryObject qo) {
        int count = permissionMapper.selectForCount(qo);
        if (count == 0) {
            return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(),
                    Collections.emptyList(), count);
        }
        int a = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if (qo.getCurrentPage() > a) {
            qo.setCurrentPage(a);
        }
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), permissions, count);


    }
     @Autowired
    ApplicationContext ctx;

    @Override
    public void reload() {
        List<Permission> permissions = permissionMapper.selectAll();
        ArrayList<String> list = new ArrayList<>();
        for (Permission p : permissions) {
            list.add(p.getExpression());

        }
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = beans.values();
        for (Object controller : controllers) {
            Method[] methods = controller.getClass().getDeclaredMethods();
            for (Method method : methods) {
                RequiredPermission requiredPermission = method.getAnnotation(RequiredPermission.class);
                if (requiredPermission == null) {
                    continue;
                }
                String expression = requiredPermission.expression();
                String name = requiredPermission.name();
                if (!list.contains(expression)) {
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    permissionMapper.insert(permission);

                }
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectByEmpId(id);
    }
}


